package org.dromara.patrol.controller;

import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.patrol.domain.bo.PatrolTemplateNodeBo;
import org.dromara.patrol.domain.vo.PatrolTemplateNodeVo;
import org.dromara.patrol.service.IPatrolTemplateNodeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 巡店管理-巡店模板分类（评分项）配置
 * 前端访问路由地址为:/patrol/templateNode
 *
 * @author LionLi
 * @date 2024-03-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/template/node")
public class PatrolTemplateNodeController extends BaseController {

    private final IPatrolTemplateNodeService patrolTemplateNodeService;

    /**
     * 查询巡店管理-巡店模板分类（评分项）配置列表
     */
//    @SaCheckPermission("patrol:templateNode:list")
    @GetMapping("/list")
    public TableDataInfo<PatrolTemplateNodeVo> list(PatrolTemplateNodeBo bo, PageQuery pageQuery) {
        return patrolTemplateNodeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡店管理-巡店模板分类（评分项）配置列表
     */
    @SaCheckPermission("patrol:templateNode:export")
    @Log(title = "巡店管理-巡店模板分类（评分项）配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatrolTemplateNodeBo bo, HttpServletResponse response) {
        List<PatrolTemplateNodeVo> list = patrolTemplateNodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡店管理-巡店模板分类（评分项）配置", PatrolTemplateNodeVo.class, response);
    }

    /**
     * 获取巡店管理-巡店模板分类（评分项）配置详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("patrol:templateNode:query")
    @GetMapping("/{id}")
    public R<PatrolTemplateNodeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(patrolTemplateNodeService.queryById(id));
    }

    /**
     * 新增巡店管理-巡店模板分类（评分项）配置
     */
    @SaCheckPermission("patrol:templateNode:add")
    @Log(title = "巡店管理-巡店模板分类（评分项）配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatrolTemplateNodeBo bo) {
        return toAjax(patrolTemplateNodeService.insertByBo(bo));
    }

    /**
     * 修改巡店管理-巡店模板分类（评分项）配置
     */
    @SaCheckPermission("patrol:templateNode:edit")
    @Log(title = "巡店管理-巡店模板分类（评分项）配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatrolTemplateNodeBo bo) {
        return toAjax(patrolTemplateNodeService.updateByBo(bo));
    }

    /**
     * 删除巡店管理-巡店模板分类（评分项）配置
     *
     * @param id 主键串
     */
    @SaCheckPermission("patrol:templateNode:remove")
    @Log(title = "巡店管理-巡店模板分类（评分项）配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        //校验是否存在子项
        if (patrolTemplateNodeService.hasChildByNodeId(id)) {
            return R.warn("存在下级分类或评分项,不允许删除");
        }
        return toAjax(patrolTemplateNodeService.deleteWithValidById(id));
    }


    /**
     * 分类树形结构的查询
     * @param bo
     * @return
     */
//    @SaCheckPermission("patrol:templateNode:tree")
    @GetMapping("/tree")
    public  R<List<Tree<Long>>>list(PatrolTemplateNodeBo bo) {
        List<Tree<Long>> tree = patrolTemplateNodeService.tree(bo);
        return R.ok(tree);
    }
}
